package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.ProductOrder;
import cn.stylefeng.guns.modular.system.entity.Unit;
import cn.stylefeng.guns.modular.system.mapper.ProductOrderMapper;
import cn.stylefeng.guns.modular.system.model.params.ProductOrderParam;
import cn.stylefeng.guns.modular.system.model.result.ProductOrderResult;
import cn.stylefeng.guns.modular.system.model.result.resultVO.ProductDetailList;
import  cn.stylefeng.guns.modular.system.service.ProductOrderService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 产品编号表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-06-27
 */
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {

    @Autowired
    private ProductOrderServiceImpl productOrderServiceImpl;
    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Override
    public void add(ProductOrderParam param){
        ProductOrder entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProductOrderParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProductOrderParam param){
        ProductOrder oldEntity = getOldEntity(param);
        ProductOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ProductOrderResult findBySpec(ProductOrderParam param){
        return null;
    }

    @Override
    public List<ProductDetailList> findListBySpec(ProductOrderParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProductOrderParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 查询最大/最小单位
     *
     * @param productId
     */
    @Override
    public List<Unit> getUnitList(Long productId) {
        List <Unit> unitList = productOrderMapper.getUnitLists(productId);
        return unitList;
    }

    private Serializable getKey(ProductOrderParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ProductOrder getOldEntity(ProductOrderParam param) {
        return this.getById(getKey(param));
    }

    public ProductOrder getEntity(ProductOrderParam param) {
        ProductOrder entity = new ProductOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
